import { defineStore } from "pinia";
import { getBuildingsDataApi, getDMDataApi, searchBuildingApi } from '@/service/buildings'
export const useBuildingStore = defineStore('buildingStore', {
    state() {
        return {
            tableData: [],
            isShowBuildingPanel: false, 
            panelTitle: '', // 面板标题
            openPanelType: '', // 打开面板类型
            editId: '',     // 编辑Id
            selectedCount: 0,   // 已经被选择的宿管个数
            dmData1: [],
            dmData2: [],
        }
    },
    actions: {
        async getBuildingData() {
            if (this.tableData.length) this.tableData = []; // 清空数组
            const res = await getBuildingsDataApi();
            this.tableData = res.data.data;
        },
        async getDMData(id = null) {
            const res = await getDMDataApi(id);
            const { code, data } = res.data;
            if (code === 1) {
                this.dmData1 = data;
                this.dmData2 = data;
            }
        },
        // 搜索
        async searchBuilding(searchObj) {
            // 发送网络请求
            const res = await searchBuildingApi(searchObj)
            const data = res.data.data;
            this.tableData = [];    // 清空数组
            if (!data.id) return;
            this.tableData.push(data);
        }
    }
})
